home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / LiveFastStartServer / EnableIPReuseAddrSample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-30  |  2.1 KB  |  70 lines  |  [TEXT/CWIE]

  1. /*
  2.     File: EnableIPReuseAddrSample.c
  3.     By:        Rich Kubota
  4.             Developer Technical Support
  5.     
  6.     Purpose: Demonstrate the use of the OTOptionManagement call to tell a TCP
  7.             endpoint that the address may be reused immediately after closing
  8.             the endpoint.  This allows the endpoint to be reopened to the
  9.             same address.  Under OT, TCP has a 2-minute timeout on a binding 
  10.             after a connection has closed before the same port can be bound 
  11.             to again. This prevents data from stale connections from 
  12.             corrupting a new one. 
  13. */
  14. #include <OpenTransport.h>            // open transport files            
  15. #include <OpenTptInternet.h>
  16.  
  17. OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
  18.  
  19. /* Input: ep - endpointref on which to negotiate the option
  20.             enableReuseIPMode - desired option setting - true/false
  21.    Return: kOTNoError indicates that the option was successfully negotiated
  22.                OSStatus is an error if < 0, otherwise, the status field is
  23.                returned and is > 0.
  24.        
  25.        IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
  26.                this code will not function as desired
  27. */
  28.  
  29.  
  30. OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
  31.  
  32. {
  33.     UInt8        buf[kOTFourByteOptionSize];    // define buffer for fourByte Option size
  34.     TOption*    opt;                        // option ptr to make items easier to access
  35.     TOptMgmt    req;
  36.     TOptMgmt    ret;
  37.     OSStatus    err;
  38.     
  39.     if (!OTIsSynchronous(ep))
  40.     {
  41.         return (-1);
  42.     }
  43.     opt = (TOption*)buf;                    // set option ptr to buffer
  44.     req.opt.buf    = buf;
  45.     req.opt.len    = sizeof(buf);
  46.     req.flags    = T_NEGOTIATE;                // negotiate for option
  47.  
  48.     ret.opt.buf = buf;
  49.     ret.opt.maxlen = kOTFourByteOptionSize;
  50.  
  51.     opt->level    = INET_IP;                    // dealing with an IP Level function
  52.     opt->name    = IP_REUSEADDR;
  53.     opt->len    = kOTFourByteOptionSize;
  54.     opt->status = 0;
  55.     *(UInt32*)opt->value = enableReuseIPMode;        // set the desired option level, true or false
  56.  
  57.     err = OTOptionManagement(ep, &req, &ret);
  58.     
  59.         // if no error then return the option status value
  60.     if (err == kOTNoError)
  61.     {
  62.         if (opt->status != T_SUCCESS)
  63.             err = opt->status;
  64.         else
  65.             err = kOTNoError;
  66.     }
  67.                 
  68.     return err;
  69. }
  70.